__author__ = 'st316'
'''
Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.
'''


class Solution:
    # @param A, a list of integers
    # @param target, an integer to be searched
    # @return an integer
    def search(self, A, target):
        if not A:
            return -1
        start = 0
        end = len(A) - 1
        mid = (start + end) / 2
        if target == A[end]:
            return end
        if target == A[start]:
            return start
        if target == A[mid]:
            return mid
        if A[mid] < A[end]:
            if A[mid] <= target <= A[end]:
                return self.bsearch(A, mid, end, target)
            else:
                return self.searchX(A, start, mid - 1, target)
        if A[mid] > A[start]:
            if A[start] <= target <= A[mid]:
                return self.bsearch(A, start, mid, target)
            else:
                return self.searchX(A, mid, end, target)
        return -1

    def bsearch(self, A, start, end, target):
        while start <= end:
            mid = (start + end) / 2
            if target == A[mid]:
                return mid
            elif target < A[mid]:
                end = mid - 1
            else:
                start = mid + 1
        return -1

    def searchX(self, A, start, end, target):
        if not A:
            return -1
        mid = (start + end) / 2
        if target == A[end]:
            return end
        if target == A[start]:
            return start
        if target == A[mid]:
            return mid
        if A[mid] < A[end]:
            if A[mid] <= target <= A[end]:
                return self.bsearch(A, mid, end, target)
            else:
                return self.searchX(A, start, mid - 1, target)
        if A[mid] > A[start]:
            if A[start] <= target <= A[mid]:
                return self.bsearch(A, start, mid, target)
            else:
                return self.searchX(A, mid, end, target)
        return -1


if __name__ == '__main__':
    def a():
        def b():
            return 1
        return b()+1
    print a()
    # s = Solution()
    # print s.search([1,2,23,4], 1)